<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>JDBC | ai_喝可乐's Blog</title><meta name="author" content="AIHCOLA"><meta name="copyright" content="AIHCOLA"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="JDBC  掌握JDBC的的CRUD 理解JDBC中各个对象的作用 掌握Druid的使用   1，JDBC概述在开发中我们使用的是java语言，那么势必要通过java语言操作数据库中的数据。这就是接下来要学习的JDBC。 1.1  JDBC概念 JDBC   就是使用Java语言操作关系型数据库的一套API 全称：( Java DataBase Connectivity ) Java 数据库连接">
<meta property="og:type" content="article">
<meta property="og:title" content="JDBC">
<meta property="og:url" content="https://aihcola.github.io/2023/05/05/JDBC/index.html">
<meta property="og:site_name" content="ai_喝可乐&#39;s Blog">
<meta property="og:description" content="JDBC  掌握JDBC的的CRUD 理解JDBC中各个对象的作用 掌握Druid的使用   1，JDBC概述在开发中我们使用的是java语言，那么势必要通过java语言操作数据库中的数据。这就是接下来要学习的JDBC。 1.1  JDBC概念 JDBC   就是使用Java语言操作关系型数据库的一套API 全称：( Java DataBase Connectivity ) Java 数据库连接">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://picsum.photos/seed/d/3456/2304">
<meta property="article:published_time" content="2023-05-05T09:43:16.406Z">
<meta property="article:modified_time" content="2023-05-07T00:23:36.305Z">
<meta property="article:author" content="AIHCOLA">
<meta property="article:tag" content="JavaWeb">
<meta property="article:tag" content="JavaEE">
<meta property="article:tag" content="JDBC">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://picsum.photos/seed/d/3456/2304"><link rel="shortcut icon" href="/img/dog.jpeg"><link rel="canonical" href="https://aihcola.github.io/2023/05/05/JDBC/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'JDBC',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-05-07 08:23:36'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/mycss.css"><meta name="generator" content="Hexo 6.3.0"></head><body><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pace-js/themes/blue/pace-theme-minimal.min.css"/><script src="https://cdn.jsdelivr.net/npm/pace-js/pace.min.js"></script><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/dog.jpeg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">4</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">8</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-book"></i><span> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/timeline/"><i class="fa-fw fa fa-bell"></i><span> 日志</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://picsum.photos/seed/sunshine/4912/3264')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">ai_喝可乐's Blog</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-book"></i><span> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-list"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/timeline/"><i class="fa-fw fa fa-bell"></i><span> 日志</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">JDBC</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-05-05T09:43:16.406Z" title="发表于 2023-05-05 17:43:16">2023-05-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-05-07T00:23:36.305Z" title="更新于 2023-05-07 08:23:36">2023-05-07</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/Javaweb/">Javaweb</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="JDBC"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="JDBC"><a href="#JDBC" class="headerlink" title="JDBC"></a>JDBC</h2><blockquote>
<ul>
<li>掌握JDBC的的CRUD</li>
<li>理解JDBC中各个对象的作用</li>
<li>掌握Druid的使用</li>
</ul>
</blockquote>
<h2 id="1，JDBC概述"><a href="#1，JDBC概述" class="headerlink" title="1，JDBC概述"></a>1，JDBC概述</h2><p>在开发中我们使用的是java语言，那么势必要通过java语言操作数据库中的数据。这就是接下来要学习的JDBC。</p>
<h3 id="1-1-JDBC概念"><a href="#1-1-JDBC概念" class="headerlink" title="1.1  JDBC概念"></a>1.1  JDBC概念</h3><blockquote>
<p>JDBC   就是使用Java语言操作关系型数据库的一套API</p>
<p>全称：( Java DataBase Connectivity ) Java 数据库连接</p>
</blockquote>
<img src= "" data-lazy-src="assets/image-20210725130537815.png" alt="image-20210725130537815" style="zoom:80%;" />

<p>我们开发的同一套Java代码是无法操作不同的关系型数据库，因为每一个关系型数据库的底层实现细节都不一样。如果这样，问题就很大了，在公司中可以在开发阶段使用的是MySQL数据库，而上线时公司最终选用oracle数据库，我们就需要对代码进行大批量修改，这显然并不是我们想看到的。我们要做到的是同一套Java代码操作不同的关系型数据库，而此时sun公司就指定了一套标准接口（JDBC），JDBC中定义了所有操作关系型数据库的规则。众所周知接口是无法直接使用的，我们需要使用接口的实现类，而这套实现类（称之为：驱动）就由各自的数据库厂商给出。</p>
<h3 id="1-2-JDBC本质"><a href="#1-2-JDBC本质" class="headerlink" title="1.2  JDBC本质"></a>1.2  JDBC本质</h3><ul>
<li>官方（sun公司）定义的一套操作所有关系型数据库的规则，即接口</li>
<li>各个数据库厂商去实现这套接口，提供数据库驱动jar包</li>
<li>我们可以使用这套接口（JDBC）编程，真正执行的代码是驱动jar包中的实现类</li>
</ul>
<h3 id="1-3-JDBC好处"><a href="#1-3-JDBC好处" class="headerlink" title="1.3  JDBC好处"></a>1.3  JDBC好处</h3><ul>
<li>各数据库厂商使用相同的接口，Java代码不需要针对不同数据库分别开发</li>
<li>可随时替换底层数据库，访问数据库的Java代码基本不变</li>
</ul>
<p>以后编写操作数据库的代码只需要面向JDBC（接口），操作哪儿个关系型数据库就需要导入该数据库的驱动包，如需要操作MySQL数据库，就需要再项目中导入MySQL数据库的驱动包。如下图就是MySQL驱动包</p>
<img src= "" data-lazy-src="assets/image-20210725133015535.png" alt="image-20210725133015535" style="zoom:90%;" />

<h2 id="2，JDBC快速入门"><a href="#2，JDBC快速入门" class="headerlink" title="2，JDBC快速入门"></a>2，JDBC快速入门</h2><p>先来看看通过Java操作数据库的流程</p>
<img src= "" data-lazy-src="assets/image-20210725163745153.png" alt="image-20210725163745153" style="zoom:80%;" />

<p>第一步：编写Java代码</p>
<p>第二步：Java代码将SQL发送到MySQL服务端</p>
<p>第三步：MySQL服务端接收到SQL语句并执行该SQL语句</p>
<p>第四步：将SQL语句执行的结果返回给Java代码</p>
<h3 id="2-1-编写代码步骤"><a href="#2-1-编写代码步骤" class="headerlink" title="2.1  编写代码步骤"></a>2.1  编写代码步骤</h3><ul>
<li><p>创建工程，导入驱动jar包</p>
<img src= "" data-lazy-src="assets/image-20210725133015535.png" alt="image-20210725133015535" style="zoom:90%;" />
</li>
<li><p>注册驱动</p>
</li>
</ul>
  <figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">Class.forName(&quot;com.mysql.jdbc.Driver&quot;);</span><br></pre></td></tr></table></figure>

<ul>
<li><p>获取连接</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">Connection conn <span class="operator">=</span> DriverManager.getConnection(url, username, password);</span><br></pre></td></tr></table></figure>

<p>Java代码需要发送SQL给MySQL服务端，就需要先建立连接</p>
</li>
<li><p>定义SQL语句</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">String <span class="keyword">sql</span> <span class="operator">=</span>  “<span class="keyword">update</span>…” ;</span><br></pre></td></tr></table></figure>
</li>
<li><p>获取执行SQL对象</p>
<p>执行SQL语句需要SQL执行对象，而这个执行对象就是Statement对象</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">Statement stmt <span class="operator">=</span> conn.createStatement();</span><br></pre></td></tr></table></figure>
</li>
<li><p>执行SQL</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">stmt.executeUpdate(<span class="keyword">sql</span>);  </span><br></pre></td></tr></table></figure>
</li>
<li><p>处理返回结果</p>
</li>
<li><p>释放资源</p>
</li>
</ul>
<h3 id="2-2-具体操作"><a href="#2-2-具体操作" class="headerlink" title="2.2  具体操作"></a>2.2  具体操作</h3><ul>
<li>创建新的空的项目</li>
</ul>
<img src= "" data-lazy-src="assets/image-20210725165156501.png" alt="image-20210725165156501" style="zoom:70%;" />

<ul>
<li>定义项目的名称，并指定位置</li>
</ul>
<img src= "" data-lazy-src="assets/image-20210725165220829.png" alt="image-20210725165220829" style="zoom:70%;" />

<ul>
<li>对项目进行设置，JDK版本、编译版本</li>
</ul>
<img src= "" data-lazy-src="assets/image-20210725165349564.png" alt="image-20210725165349564" style="zoom:70%;" />

<ul>
<li>创建模块，指定模块的名称及位置</li>
</ul>
<img src= "" data-lazy-src="assets/image-20210725165536898.png" alt="image-20210725165536898" style="zoom:70%;" />

<ul>
<li><p>导入驱动包</p>
<p>将mysql的驱动包放在模块下的lib目录（随意命名）下，并将该jar包添加为库文件</p>
</li>
</ul>
<img src= "" data-lazy-src="assets/image-20210725165657556.png" alt="image-20210725165657556" style="zoom:80%;" />

<ul>
<li>在添加为库文件的时候，有如下三个选项<ul>
<li>Global Library  ： 全局有效</li>
<li>Project Library :   项目有效</li>
<li>Module Library ： 模块有效</li>
</ul>
</li>
</ul>
<img src= "" data-lazy-src="assets/image-20210725165751273.png" alt="image-20210725165751273" style="zoom:80%;" />

<ul>
<li>在src下创建类</li>
</ul>
<img src= "" data-lazy-src="assets/image-20210725170004319.png" alt="image-20210725170004319" style="zoom:70%;" />

<ul>
<li>编写代码如下</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * JDBC快速入门</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDBCDemo</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">        <span class="comment">//1. 注册驱动</span></span><br><span class="line">        <span class="comment">//Class.forName(&quot;com.mysql.jdbc.Driver&quot;);</span></span><br><span class="line">        <span class="comment">//2. 获取连接</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql://127.0.0.1:3306/db1&quot;</span>;</span><br><span class="line">        <span class="type">String</span> <span class="variable">username</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">        <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;1234&quot;</span>;</span><br><span class="line">        <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> DriverManager.getConnection(url, username, password);</span><br><span class="line">        <span class="comment">//3. 定义sql</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;update account set money = 2000 where id = 1&quot;</span>;</span><br><span class="line">        <span class="comment">//4. 获取执行sql的对象 Statement</span></span><br><span class="line">        <span class="type">Statement</span> <span class="variable">stmt</span> <span class="operator">=</span> conn.createStatement();</span><br><span class="line">        <span class="comment">//5. 执行sql</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> stmt.executeUpdate(sql);<span class="comment">//受影响的行数</span></span><br><span class="line">        <span class="comment">//6. 处理结果</span></span><br><span class="line">        System.out.println(count);</span><br><span class="line">        <span class="comment">//7. 释放资源</span></span><br><span class="line">        stmt.close();</span><br><span class="line">        conn.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="3，JDBC-API详解"><a href="#3，JDBC-API详解" class="headerlink" title="3，JDBC API详解"></a>3，JDBC API详解</h2><h3 id="3-1-DriverManager"><a href="#3-1-DriverManager" class="headerlink" title="3.1  DriverManager"></a>3.1  DriverManager</h3><p>DriverManager（驱动管理类）作用：</p>
<ul>
<li><p>注册驱动</p>
<p><img src= "" data-lazy-src="/assets/image-20210725171339346.png" alt="image-20210725171339346"></p>
<p>registerDriver方法是用于注册驱动的，但是我们之前做的入门案例并不是这样写的。而是如下实现</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">Class.forName(&quot;com.mysql.jdbc.Driver&quot;);</span><br></pre></td></tr></table></figure>

<p>我们查询MySQL提供的Driver类，看它是如何实现的，源码如下：</p>
<img src= "" data-lazy-src="assets/image-20210725171635432.png" alt="image-20210725171635432" style="zoom:70%;" />

<p>在该类中的静态代码块中已经执行了 <code>DriverManager</code> 对象的 <code>registerDriver()</code> 方法进行驱动的注册了，那么我们只需要加载 <code>Driver</code> 类，该静态代码块就会执行。而 <code>Class.forName(&quot;com.mysql.jdbc.Driver&quot;);</code> 就可以加载 <code>Driver</code> 类。</p>
<blockquote>
<p>&#x3D;&#x3D;提示：&#x3D;&#x3D;</p>
<ul>
<li>MySQL 5之后的驱动包，可以省略注册驱动的步骤</li>
<li>自动加载jar包中META-INF&#x2F;services&#x2F;java.sql.Driver文件中的驱动类</li>
</ul>
</blockquote>
</li>
<li><p>获取数据库连接</p>
<p><img src= "" data-lazy-src="/assets/image-20210725171355278.png" alt="image-20210725171355278"></p>
<p>参数说明：</p>
<ul>
<li><p>url ： 连接路径</p>
<blockquote>
<p>语法：jdbc:mysql:&#x2F;&#x2F;ip地址(域名):端口号&#x2F;数据库名称?参数键值对1&amp;参数键值对2…</p>
<p>示例：jdbc:mysql:&#x2F;&#x2F;127.0.0.1:3306&#x2F;db1</p>
<p>&#x3D;&#x3D;细节：&#x3D;&#x3D;</p>
<ul>
<li><p>如果连接的是本机mysql服务器，并且mysql服务默认端口是3306，则url可以简写为：jdbc:mysql:&#x2F;&#x2F;&#x2F;数据库名称?参数键值对</p>
</li>
<li><p>配置 useSSL&#x3D;false 参数，禁用安全连接方式，解决警告提示</p>
</li>
</ul>
</blockquote>
</li>
<li><p>user ：用户名</p>
</li>
<li><p>poassword ：密码</p>
</li>
</ul>
</li>
</ul>
<h3 id="3-2-Connection"><a href="#3-2-Connection" class="headerlink" title="3.2  Connection"></a>3.2  Connection</h3><p>Connection（数据库连接对象）作用：</p>
<ul>
<li>获取执行 SQL 的对象</li>
<li>管理事务</li>
</ul>
<h4 id="3-2-1-获取执行对象"><a href="#3-2-1-获取执行对象" class="headerlink" title="3.2.1  获取执行对象"></a>3.2.1  获取执行对象</h4><ul>
<li><p>普通执行SQL对象</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">Statement createStatement()</span><br></pre></td></tr></table></figure>

<p>入门案例中就是通过该方法获取的执行对象。</p>
</li>
<li><p>预编译SQL的执行SQL对象：防止SQL注入</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">PreparedStatement  prepareStatement(<span class="keyword">sql</span>)</span><br></pre></td></tr></table></figure>

<p>通过这种方式获取的 <code>PreparedStatement</code> SQL语句执行对象是我们一会重点要进行讲解的，它可以防止SQL注入。</p>
</li>
<li><p>执行存储过程的对象</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">CallableStatement prepareCall(<span class="keyword">sql</span>)</span><br></pre></td></tr></table></figure>

<p>通过这种方式获取的 <code>CallableStatement</code> 执行对象是用来执行存储过程的，而存储过程在MySQL中不常用，所以这个我们将不进行讲解。</p>
</li>
</ul>
<h4 id="3-2-2-事务管理"><a href="#3-2-2-事务管理" class="headerlink" title="3.2.2  事务管理"></a>3.2.2  事务管理</h4><p>先回顾一下MySQL事务管理的操作：</p>
<ul>
<li>开启事务 ： BEGIN; 或者 START TRANSACTION;</li>
<li>提交事务 ： COMMIT;</li>
<li>回滚事务 ： ROLLBACK;</li>
</ul>
<blockquote>
<p>MySQL默认是自动提交事务</p>
</blockquote>
<p>接下来学习JDBC事务管理的方法。</p>
<p>Connection几口中定义了3个对应的方法：</p>
<ul>
<li><p>开启事务</p>
<p><img src= "" data-lazy-src="/assets/image-20210725173444628.png" alt="image-20210725173444628"></p>
<p>参与autoCommit 表示是否自动提交事务，true表示自动提交事务，false表示手动提交事务。而开启事务需要将该参数设为为false。</p>
</li>
<li><p>提交事务</p>
<p><img src= "" data-lazy-src="/assets/image-20210725173618636.png" alt="image-20210725173618636"></p>
</li>
<li><p>回滚事务</p>
<p><img src= "" data-lazy-src="/assets/image-20210725173648674.png" alt="image-20210725173648674"></p>
</li>
</ul>
<p>具体代码实现如下：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * JDBC API 详解：Connection</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public class JDBCDemo3_Connection &#123;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> void main(String[] args) throws Exception &#123;</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span><span class="number">1.</span> 注册驱动</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span>Class.forName(&quot;com.mysql.jdbc.Driver&quot;);</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span><span class="number">2.</span> 获取连接：如果连接的是本机mysql并且端口是默认的 <span class="number">3306</span> 可以简化书写</span><br><span class="line">        String url <span class="operator">=</span> &quot;jdbc:mysql:///db1?useSSL=false&quot;;</span><br><span class="line">        String username <span class="operator">=</span> &quot;root&quot;;</span><br><span class="line">        String password <span class="operator">=</span> &quot;1234&quot;;</span><br><span class="line">        Connection conn <span class="operator">=</span> DriverManager.getConnection(url, username, password);</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span><span class="number">3.</span> 定义<span class="keyword">sql</span></span><br><span class="line">        String sql1 <span class="operator">=</span> &quot;update account set money = 3000 where id = 1&quot;;</span><br><span class="line">        String sql2 <span class="operator">=</span> &quot;update account set money = 3000 where id = 2&quot;;</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span><span class="number">4.</span> 获取执行<span class="keyword">sql</span>的对象 Statement</span><br><span class="line">        Statement stmt <span class="operator">=</span> conn.createStatement();</span><br><span class="line"></span><br><span class="line">        try &#123;</span><br><span class="line">            <span class="operator">/</span><span class="operator">/</span> <span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span>开启事务<span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span></span><br><span class="line">            conn.setAutoCommit(<span class="literal">false</span>);</span><br><span class="line">            <span class="operator">/</span><span class="operator">/</span><span class="number">5.</span> 执行<span class="keyword">sql</span></span><br><span class="line">            <span class="type">int</span> count1 <span class="operator">=</span> stmt.executeUpdate(sql1);<span class="operator">/</span><span class="operator">/</span>受影响的行数</span><br><span class="line">            <span class="operator">/</span><span class="operator">/</span><span class="number">6.</span> 处理结果</span><br><span class="line">            System.out.println(count1);</span><br><span class="line">            <span class="type">int</span> i <span class="operator">=</span> <span class="number">3</span><span class="operator">/</span><span class="number">0</span>;</span><br><span class="line">            <span class="operator">/</span><span class="operator">/</span><span class="number">5.</span> 执行<span class="keyword">sql</span></span><br><span class="line">            <span class="type">int</span> count2 <span class="operator">=</span> stmt.executeUpdate(sql2);<span class="operator">/</span><span class="operator">/</span>受影响的行数</span><br><span class="line">            <span class="operator">/</span><span class="operator">/</span><span class="number">6.</span> 处理结果</span><br><span class="line">            System.out.println(count2);</span><br><span class="line"></span><br><span class="line">            <span class="operator">/</span><span class="operator">/</span> <span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span>提交事务<span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span></span><br><span class="line">            <span class="operator">/</span><span class="operator">/</span>程序运行到此处，说明没有出现任何问题，则需求提交事务</span><br><span class="line">            conn.commit();</span><br><span class="line">        &#125; catch (Exception e) &#123;</span><br><span class="line">            <span class="operator">/</span><span class="operator">/</span> <span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span>回滚事务<span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span><span class="operator">=</span></span><br><span class="line">            <span class="operator">/</span><span class="operator">/</span>程序在出现异常时会执行到这个地方，此时就需要回滚事务</span><br><span class="line">            conn.rollback();</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span><span class="number">7.</span> 释放资源</span><br><span class="line">        stmt.close();</span><br><span class="line">        conn.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-3-Statement"><a href="#3-3-Statement" class="headerlink" title="3.3  Statement"></a>3.3  Statement</h3><h4 id="3-3-1-概述"><a href="#3-3-1-概述" class="headerlink" title="3.3.1  概述"></a>3.3.1  概述</h4><p>Statement对象的作用就是用来执行SQL语句。而针对不同类型的SQL语句使用的方法也不一样。</p>
<ul>
<li><p>执行DDL、DML语句</p>
<p><img src= "" data-lazy-src="/assets/image-20210725175151272.png" alt="image-20210725175151272"></p>
</li>
<li><p>执行DQL语句</p>
<img src= "" data-lazy-src="assets/image-20210725175131533.png" alt="image-20210725175131533" style="zoom:80%;" />

<p>该方法涉及到了 <code>ResultSet</code> 对象，而这个对象我们还没有学习，一会再重点讲解。</p>
</li>
</ul>
<h4 id="3-3-2-代码实现"><a href="#3-3-2-代码实现" class="headerlink" title="3.3.2  代码实现"></a>3.3.2  代码实现</h4><ul>
<li><p>执行DML语句</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 执行DML语句</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testDML</span><span class="params">()</span> <span class="keyword">throws</span>  Exception &#123;</span><br><span class="line">    <span class="comment">//1. 注册驱动</span></span><br><span class="line">    <span class="comment">//Class.forName(&quot;com.mysql.jdbc.Driver&quot;);</span></span><br><span class="line">    <span class="comment">//2. 获取连接：如果连接的是本机mysql并且端口是默认的 3306 可以简化书写</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql:///db1?useSSL=false&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">username</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;1234&quot;</span>;</span><br><span class="line">    <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> DriverManager.getConnection(url, username, password);</span><br><span class="line">    <span class="comment">//3. 定义sql</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;update account set money = 3000 where id = 1&quot;</span>;</span><br><span class="line">    <span class="comment">//4. 获取执行sql的对象 Statement</span></span><br><span class="line">    <span class="type">Statement</span> <span class="variable">stmt</span> <span class="operator">=</span> conn.createStatement();</span><br><span class="line">    <span class="comment">//5. 执行sql</span></span><br><span class="line">    <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> stmt.executeUpdate(sql);<span class="comment">//执行完DML语句，受影响的行数</span></span><br><span class="line">    <span class="comment">//6. 处理结果</span></span><br><span class="line">    <span class="comment">//System.out.println(count);</span></span><br><span class="line">    <span class="keyword">if</span>(count &gt; <span class="number">0</span>)&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;修改成功~&quot;</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;修改失败~&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//7. 释放资源</span></span><br><span class="line">    stmt.close();</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>执行DDL语句</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 执行DDL语句</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testDDL</span><span class="params">()</span> <span class="keyword">throws</span>  Exception &#123;</span><br><span class="line">    <span class="comment">//1. 注册驱动</span></span><br><span class="line">    <span class="comment">//Class.forName(&quot;com.mysql.jdbc.Driver&quot;);</span></span><br><span class="line">    <span class="comment">//2. 获取连接：如果连接的是本机mysql并且端口是默认的 3306 可以简化书写</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql:///db1?useSSL=false&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">username</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;1234&quot;</span>;</span><br><span class="line">    <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> DriverManager.getConnection(url, username, password);</span><br><span class="line">    <span class="comment">//3. 定义sql</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;drop database db2&quot;</span>;</span><br><span class="line">    <span class="comment">//4. 获取执行sql的对象 Statement</span></span><br><span class="line">    <span class="type">Statement</span> <span class="variable">stmt</span> <span class="operator">=</span> conn.createStatement();</span><br><span class="line">    <span class="comment">//5. 执行sql</span></span><br><span class="line">    <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> stmt.executeUpdate(sql);<span class="comment">//执行完DDL语句，可能是0</span></span><br><span class="line">    <span class="comment">//6. 处理结果</span></span><br><span class="line">    System.out.println(count);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//7. 释放资源</span></span><br><span class="line">    stmt.close();</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意：</p>
<ul>
<li>以后开发很少使用java代码操作DDL语句</li>
</ul>
</blockquote>
</li>
</ul>
<h3 id="3-4-ResultSet"><a href="#3-4-ResultSet" class="headerlink" title="3.4  ResultSet"></a>3.4  ResultSet</h3><h4 id="3-4-1-概述"><a href="#3-4-1-概述" class="headerlink" title="3.4.1  概述"></a>3.4.1  概述</h4><p>ResultSet（结果集对象）作用：</p>
<ul>
<li>&#x3D;&#x3D;封装了SQL查询语句的结果。&#x3D;&#x3D;</li>
</ul>
<p>而执行了DQL语句后就会返回该对象，对应执行DQL语句的方法如下：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">ResultSet  executeQuery(<span class="keyword">sql</span>)：执行DQL 语句，返回 ResultSet 对象</span><br></pre></td></tr></table></figure>

<p>那么我们就需要从 <code>ResultSet</code> 对象中获取我们想要的数据。<code>ResultSet</code> 对象提供了操作查询结果数据的方法，如下：</p>
<blockquote>
<p>boolean  next()</p>
<ul>
<li>将光标从当前位置向前移动一行 </li>
<li>判断当前行是否为有效行</li>
</ul>
<p>方法返回值说明：</p>
<ul>
<li>true  ： 有效航，当前行有数据</li>
<li>false ： 无效行，当前行没有数据</li>
</ul>
</blockquote>
<blockquote>
<p>xxx  getXxx(参数)：获取数据</p>
<ul>
<li>xxx : 数据类型；如： int getInt(参数) ；String getString(参数)</li>
<li>参数<ul>
<li>int类型的参数：列的编号，从1开始</li>
<li>String类型的参数： 列的名称</li>
</ul>
</li>
</ul>
</blockquote>
<p>如下图为执行SQL语句后的结果</p>
<img src= "" data-lazy-src="assets/image-20210725181320813.png" alt="image-20210725181320813" style="zoom:80%;" />

<p>一开始光标指定于第一行前，如图所示红色箭头指向于表头行。当我们调用了 <code>next()</code> 方法后，光标就下移到第一行数据，并且方法返回true，此时就可以通过 <code>getInt(&quot;id&quot;)</code> 获取当前行id字段的值，也可以通过 <code>getString(&quot;name&quot;)</code> 获取当前行name字段的值。如果想获取下一行的数据，继续调用 <code>next()</code>  方法，以此类推。</p>
<h4 id="3-4-2-代码实现"><a href="#3-4-2-代码实现" class="headerlink" title="3.4.2  代码实现"></a>3.4.2  代码实现</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 执行DQL</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testResultSet</span><span class="params">()</span> <span class="keyword">throws</span>  Exception &#123;</span><br><span class="line">    <span class="comment">//1. 注册驱动</span></span><br><span class="line">    <span class="comment">//Class.forName(&quot;com.mysql.jdbc.Driver&quot;);</span></span><br><span class="line">    <span class="comment">//2. 获取连接：如果连接的是本机mysql并且端口是默认的 3306 可以简化书写</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql:///db1?useSSL=false&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">username</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;1234&quot;</span>;</span><br><span class="line">    <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> DriverManager.getConnection(url, username, password);</span><br><span class="line">    <span class="comment">//3. 定义sql</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;select * from account&quot;</span>;</span><br><span class="line">    <span class="comment">//4. 获取statement对象</span></span><br><span class="line">    <span class="type">Statement</span> <span class="variable">stmt</span> <span class="operator">=</span> conn.createStatement();</span><br><span class="line">    <span class="comment">//5. 执行sql</span></span><br><span class="line">    <span class="type">ResultSet</span> <span class="variable">rs</span> <span class="operator">=</span> stmt.executeQuery(sql);</span><br><span class="line">    <span class="comment">//6. 处理结果， 遍历rs中的所有数据</span></span><br><span class="line">    <span class="comment">/* // 6.1 光标向下移动一行，并且判断当前行是否有数据</span></span><br><span class="line"><span class="comment">        while (rs.next())&#123;</span></span><br><span class="line"><span class="comment">            //6.2 获取数据  getXxx()</span></span><br><span class="line"><span class="comment">            int id = rs.getInt(1);</span></span><br><span class="line"><span class="comment">            String name = rs.getString(2);</span></span><br><span class="line"><span class="comment">            double money = rs.getDouble(3);</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            System.out.println(id);</span></span><br><span class="line"><span class="comment">            System.out.println(name);</span></span><br><span class="line"><span class="comment">            System.out.println(money);</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            System.out.println(&quot;--------------&quot;);</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        &#125;*/</span></span><br><span class="line">    <span class="comment">// 6.1 光标向下移动一行，并且判断当前行是否有数据</span></span><br><span class="line">    <span class="keyword">while</span> (rs.next())&#123;</span><br><span class="line">        <span class="comment">//6.2 获取数据  getXxx()</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">id</span> <span class="operator">=</span> rs.getInt(<span class="string">&quot;id&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> rs.getString(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">        <span class="type">double</span> <span class="variable">money</span> <span class="operator">=</span> rs.getDouble(<span class="string">&quot;money&quot;</span>);</span><br><span class="line"></span><br><span class="line">        System.out.println(id);</span><br><span class="line">        System.out.println(name);</span><br><span class="line">        System.out.println(money);</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;--------------&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//7. 释放资源</span></span><br><span class="line">    rs.close();</span><br><span class="line">    stmt.close();</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-5-案例"><a href="#3-5-案例" class="headerlink" title="3.5  案例"></a>3.5  案例</h3><ul>
<li><p>需求：查询account账户表数据，封装为Account对象中，并且存储到ArrayList集合中</p>
<img src= "" data-lazy-src="assets/image-20210725182352433.png" alt="image-20210725182352433" style="zoom:80%;" />
</li>
<li><p>代码实现</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 查询account账户表数据，封装为Account对象中，并且存储到ArrayList集合中</span></span><br><span class="line"><span class="comment">  * 1. 定义实体类Account</span></span><br><span class="line"><span class="comment">  * 2. 查询数据，封装到Account对象中</span></span><br><span class="line"><span class="comment">  * 3. 将Account对象存入ArrayList集合中</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testResultSet2</span><span class="params">()</span> <span class="keyword">throws</span>  Exception &#123;</span><br><span class="line">    <span class="comment">//1. 注册驱动</span></span><br><span class="line">    <span class="comment">//Class.forName(&quot;com.mysql.jdbc.Driver&quot;);</span></span><br><span class="line">    <span class="comment">//2. 获取连接：如果连接的是本机mysql并且端口是默认的 3306 可以简化书写</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql:///db1?useSSL=false&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">username</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;1234&quot;</span>;</span><br><span class="line">    <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> DriverManager.getConnection(url, username, password);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//3. 定义sql</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;select * from account&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//4. 获取statement对象</span></span><br><span class="line">    <span class="type">Statement</span> <span class="variable">stmt</span> <span class="operator">=</span> conn.createStatement();</span><br><span class="line"></span><br><span class="line">    <span class="comment">//5. 执行sql</span></span><br><span class="line">    <span class="type">ResultSet</span> <span class="variable">rs</span> <span class="operator">=</span> stmt.executeQuery(sql);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 创建集合</span></span><br><span class="line">    List&lt;Account&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">   </span><br><span class="line">    <span class="comment">// 6.1 光标向下移动一行，并且判断当前行是否有数据</span></span><br><span class="line">    <span class="keyword">while</span> (rs.next())&#123;</span><br><span class="line">        <span class="type">Account</span> <span class="variable">account</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Account</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//6.2 获取数据  getXxx()</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">id</span> <span class="operator">=</span> rs.getInt(<span class="string">&quot;id&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> rs.getString(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">        <span class="type">double</span> <span class="variable">money</span> <span class="operator">=</span> rs.getDouble(<span class="string">&quot;money&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//赋值</span></span><br><span class="line">        account.setId(id);</span><br><span class="line">        account.setName(name);</span><br><span class="line">        account.setMoney(money);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 存入集合</span></span><br><span class="line">        list.add(account);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    System.out.println(list);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//7. 释放资源</span></span><br><span class="line">    rs.close();</span><br><span class="line">    stmt.close();</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="3-6-PreparedStatement"><a href="#3-6-PreparedStatement" class="headerlink" title="3.6  PreparedStatement"></a>3.6  PreparedStatement</h3><blockquote>
<p>PreparedStatement作用：</p>
<ul>
<li>预编译SQL语句并执行：预防SQL注入问题</li>
</ul>
</blockquote>
<p>对上面的作用中SQL注入问题大家肯定不理解。那我们先对SQL注入进行说明.</p>
<h4 id="3-6-1-SQL注入"><a href="#3-6-1-SQL注入" class="headerlink" title="3.6.1  SQL注入"></a>3.6.1  SQL注入</h4><blockquote>
<p>SQL注入是通过操作输入来修改事先定义好的SQL语句，用以达到执行代码对服务器进行攻击的方法。</p>
</blockquote>
<p>在今天资料下的 <code>day03-JDBC\资料\2. sql注入演示</code> 中修改 <code>application.properties</code> 文件中的用户名和密码，文件内容如下：</p>
<figure class="highlight properties"><table><tr><td class="code"><pre><span class="line"><span class="attr">spring.datasource.driver-class-name</span>=<span class="string">com.mysql.cj.jdbc.Driver</span></span><br><span class="line"><span class="attr">spring.datasource.url</span>=<span class="string">jdbc:mysql://localhost:3306/test?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8</span></span><br><span class="line"><span class="attr">spring.datasource.username</span>=<span class="string">root</span></span><br><span class="line"><span class="attr">spring.datasource.password</span>=<span class="string">1234</span></span><br></pre></td></tr></table></figure>

<p>在MySQL中创建名为 <code>test</code> 的数据库</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">create</span> database test;</span><br></pre></td></tr></table></figure>

<p>在命令提示符中运行今天资料下的 <code>day03-JDBC\资料\2. sql注入演示\sql.jar</code> 这个jar包。</p>
<img src= "" data-lazy-src="assets/image-20210725184701026.png" alt="image-20210725184701026" style="zoom:80%;" /> 

<p>此时我们就能在数据库中看到user表</p>
<img src= "" data-lazy-src="assets/image-20210725184817731.png" alt="image-20210725184817731" style="zoom:80%;" />

<p>接下来在浏览器的地址栏输入 <code>localhost:8080/login.html</code> 就能看到如下页面</p>
<img src= "" data-lazy-src="assets/image-20210725185024731.png" alt="image-20210725185024731" style="zoom:80%;" />

<p>我们就可以在如上图中输入用户名和密码进行登陆。用户名和密码输入正确就登陆成功，跳转到首页。用户名和密码输入错误则给出错误提示，如下图</p>
<img src= "" data-lazy-src="assets/image-20210725185320875.png" alt="image-20210725185320875" style="zoom:80%;" />

<p>但是我可以通过输入一些特殊的字符登陆到首页。</p>
<p>用户名随意写，密码写成 <code>&#39; or &#39;1&#39; =&#39;1</code></p>
<img src= "" data-lazy-src="assets/image-20210725185603112.png" alt="image-20210725185603112" style="zoom:80%;" />

<p>这就是SQL注入漏洞，也是很危险的。当然现在市面上的系统都不会存在这种问题了，所以大家也不要尝试用这种方式去试其他的系统。</p>
<p>那么该如何解决呢？这里就可以将SQL执行对象 <code>Statement</code> 换成 <code>PreparedStatement</code> 对象。</p>
<h4 id="3-6-2-代码模拟SQL注入问题"><a href="#3-6-2-代码模拟SQL注入问题" class="headerlink" title="3.6.2  代码模拟SQL注入问题"></a>3.6.2  代码模拟SQL注入问题</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testLogin</span><span class="params">()</span> <span class="keyword">throws</span>  Exception &#123;</span><br><span class="line">    <span class="comment">//2. 获取连接：如果连接的是本机mysql并且端口是默认的 3306 可以简化书写</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql:///db1?useSSL=false&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">username</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;1234&quot;</span>;</span><br><span class="line">    <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> DriverManager.getConnection(url, username, password);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 接收用户输入 用户名和密码</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">&quot;sjdljfld&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">pwd</span> <span class="operator">=</span> <span class="string">&quot;&#x27; or &#x27;1&#x27; = &#x27;1&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;select * from tb_user where username = &#x27;&quot;</span>+name+<span class="string">&quot;&#x27; and password = &#x27;&quot;</span>+pwd+<span class="string">&quot;&#x27;&quot;</span>;</span><br><span class="line">    <span class="comment">// 获取stmt对象</span></span><br><span class="line">    <span class="type">Statement</span> <span class="variable">stmt</span> <span class="operator">=</span> conn.createStatement();</span><br><span class="line">    <span class="comment">// 执行sql</span></span><br><span class="line">    <span class="type">ResultSet</span> <span class="variable">rs</span> <span class="operator">=</span> stmt.executeQuery(sql);</span><br><span class="line">    <span class="comment">// 判断登录是否成功</span></span><br><span class="line">    <span class="keyword">if</span>(rs.next())&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;登录成功~&quot;</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;登录失败~&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//7. 释放资源</span></span><br><span class="line">    rs.close();</span><br><span class="line">    stmt.close();</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面代码是将用户名和密码拼接到sql语句中，拼接后的sql语句如下</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> tb_user <span class="keyword">where</span> username <span class="operator">=</span> <span class="string">&#x27;sjdljfld&#x27;</span> <span class="keyword">and</span> password <span class="operator">=</span> <span class="string">&#x27;&#x27;</span><span class="keyword">or</span> <span class="string">&#x27;1&#x27;</span> <span class="operator">=</span> <span class="string">&#x27;1&#x27;</span></span><br></pre></td></tr></table></figure>

<p>从上面语句可以看出条件 <code>username = &#39;sjdljfld&#39; and password = &#39;&#39;</code> 不管是否满足，而 <code>or</code> 后面的 <code>&#39;1&#39; = &#39;1&#39;</code> 是始终满足的，最终条件是成立的，就可以正常的进行登陆了。</p>
<p>接下来我们来学习PreparedStatement对象.</p>
<h4 id="3-6-3-PreparedStatement概述"><a href="#3-6-3-PreparedStatement概述" class="headerlink" title="3.6.3  PreparedStatement概述"></a>3.6.3  PreparedStatement概述</h4><blockquote>
<p>PreparedStatement作用：</p>
<ul>
<li>预编译SQL语句并执行：预防SQL注入问题</li>
</ul>
</blockquote>
<ul>
<li><p>获取 PreparedStatement 对象</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">// SQL语句中的参数值，使用？占位符替代</span></span><br><span class="line"><span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;select * from user where username = ? and password = ?&quot;</span>;</span><br><span class="line"><span class="comment">// 通过Connection对象获取，并传入对应的sql语句</span></span><br><span class="line"><span class="type">PreparedStatement</span> <span class="variable">pstmt</span> <span class="operator">=</span> conn.prepareStatement(sql);</span><br></pre></td></tr></table></figure>
</li>
<li><p>设置参数值</p>
<p>上面的sql语句中参数使用 ? 进行占位，在之前之前肯定要设置这些 ?  的值。</p>
<blockquote>
<p>PreparedStatement对象：setXxx(参数1，参数2)：给 ? 赋值</p>
<ul>
<li><p>Xxx：数据类型 ； 如 setInt (参数1，参数2)</p>
</li>
<li><p>参数：</p>
<ul>
<li><p>参数1： ？的位置编号，从1 开始</p>
</li>
<li><p>参数2： ？的值</p>
</li>
</ul>
</li>
</ul>
</blockquote>
</li>
<li><p>执行SQL语句</p>
<blockquote>
<p>executeUpdate();  执行DDL语句和DML语句</p>
<p>executeQuery();  执行DQL语句</p>
<p>&#x3D;&#x3D;注意：&#x3D;&#x3D;</p>
<ul>
<li>调用这两个方法时不需要传递SQL语句，因为获取SQL语句执行对象时已经对SQL语句进行预编译了。</li>
</ul>
</blockquote>
</li>
</ul>
<h4 id="3-6-4-使用PreparedStatement改进"><a href="#3-6-4-使用PreparedStatement改进" class="headerlink" title="3.6.4  使用PreparedStatement改进"></a>3.6.4  使用PreparedStatement改进</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"> <span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testPreparedStatement</span><span class="params">()</span> <span class="keyword">throws</span>  Exception &#123;</span><br><span class="line">    <span class="comment">//2. 获取连接：如果连接的是本机mysql并且端口是默认的 3306 可以简化书写</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql:///db1?useSSL=false&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">username</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;1234&quot;</span>;</span><br><span class="line">    <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> DriverManager.getConnection(url, username, password);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 接收用户输入 用户名和密码</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">&quot;zhangsan&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">pwd</span> <span class="operator">=</span> <span class="string">&quot;&#x27; or &#x27;1&#x27; = &#x27;1&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 定义sql</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;select * from tb_user where username = ? and password = ?&quot;</span>;</span><br><span class="line">    <span class="comment">// 获取pstmt对象</span></span><br><span class="line">    <span class="type">PreparedStatement</span> <span class="variable">pstmt</span> <span class="operator">=</span> conn.prepareStatement(sql);</span><br><span class="line">    <span class="comment">// 设置？的值</span></span><br><span class="line">    pstmt.setString(<span class="number">1</span>,name);</span><br><span class="line">    pstmt.setString(<span class="number">2</span>,pwd);</span><br><span class="line">    <span class="comment">// 执行sql</span></span><br><span class="line">    <span class="type">ResultSet</span> <span class="variable">rs</span> <span class="operator">=</span> pstmt.executeQuery();</span><br><span class="line">    <span class="comment">// 判断登录是否成功</span></span><br><span class="line">    <span class="keyword">if</span>(rs.next())&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;登录成功~&quot;</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;登录失败~&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//7. 释放资源</span></span><br><span class="line">    rs.close();</span><br><span class="line">    pstmt.close();</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行上面语句就可以发现不会出现SQL注入漏洞问题了。那么PreparedStatement又是如何解决的呢？它是将特殊字符进行了转义，转义的SQL如下：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> tb_user <span class="keyword">where</span> username <span class="operator">=</span> <span class="string">&#x27;sjdljfld&#x27;</span> <span class="keyword">and</span> password <span class="operator">=</span> <span class="string">&#x27;\&#x27;</span><span class="keyword">or</span> \<span class="string">&#x27;1\&#x27;</span> <span class="operator">=</span> \<span class="string">&#x27;1&#x27;</span></span><br></pre></td></tr></table></figure>



<h4 id="3-6-5-PreparedStatement原理"><a href="#3-6-5-PreparedStatement原理" class="headerlink" title="3.6.5  PreparedStatement原理"></a>3.6.5  PreparedStatement原理</h4><blockquote>
<p>PreparedStatement 好处：</p>
<ul>
<li>预编译SQL，性能更高</li>
<li>防止SQL注入：&#x3D;&#x3D;将敏感字符进行转义&#x3D;&#x3D;</li>
</ul>
</blockquote>
<img src= "" data-lazy-src="assets/image-20210725195756848.png" alt="image-20210725195756848" style="zoom:80%;" />

<p>Java代码操作数据库流程如图所示：</p>
<ul>
<li><p>将sql语句发送到MySQL服务器端</p>
</li>
<li><p>MySQL服务端会对sql语句进行如下操作</p>
<ul>
<li><p>检查SQL语句</p>
<p>检查SQL语句的语法是否正确。</p>
</li>
<li><p>编译SQL语句。将SQL语句编译成可执行的函数。</p>
<p>检查SQL和编译SQL花费的时间比执行SQL的时间还要长。如果我们只是重新设置参数，那么检查SQL语句和编译SQL语句将不需要重复执行。这样就提高了性能。</p>
</li>
<li><p>执行SQL语句</p>
</li>
</ul>
</li>
</ul>
<p>接下来我们通过查询日志来看一下原理。</p>
<ul>
<li><p>开启预编译功能</p>
<p>在代码中编写url时需要加上以下参数。而我们之前根本就没有开启预编译功能，只是解决了SQL注入漏洞。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">useServerPrepStmts<span class="operator">=</span><span class="literal">true</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>配置MySQL执行日志（重启mysql服务后生效）</p>
<p>在mysql配置文件（my.ini）中添加如下配置</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">log-output=FILE</span><br><span class="line">general-log=1</span><br><span class="line">general_log_file=&quot;D:\mysql.log&quot;</span><br><span class="line">slow-query-log=1</span><br><span class="line">slow_query_log_file=&quot;D:\mysql_slow.log&quot;</span><br><span class="line">long_query_time=2</span><br></pre></td></tr></table></figure>
</li>
<li><p>java测试代码如下：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * PreparedStatement原理</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testPreparedStatement2</span><span class="params">()</span> <span class="keyword">throws</span>  Exception &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//2. 获取连接：如果连接的是本机mysql并且端口是默认的 3306 可以简化书写</span></span><br><span class="line">    <span class="comment">// useServerPrepStmts=true 参数开启预编译功能</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql:///db1?useSSL=false&amp;useServerPrepStmts=true&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">username</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;1234&quot;</span>;</span><br><span class="line">    <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> DriverManager.getConnection(url, username, password);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 接收用户输入 用户名和密码</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">&quot;zhangsan&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">pwd</span> <span class="operator">=</span> <span class="string">&quot;&#x27; or &#x27;1&#x27; = &#x27;1&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 定义sql</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;select * from tb_user where username = ? and password = ?&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取pstmt对象</span></span><br><span class="line">    <span class="type">PreparedStatement</span> <span class="variable">pstmt</span> <span class="operator">=</span> conn.prepareStatement(sql);</span><br><span class="line"></span><br><span class="line">    Thread.sleep(<span class="number">10000</span>);</span><br><span class="line">    <span class="comment">// 设置？的值</span></span><br><span class="line">    pstmt.setString(<span class="number">1</span>,name);</span><br><span class="line">    pstmt.setString(<span class="number">2</span>,pwd);</span><br><span class="line">    <span class="type">ResultSet</span> <span class="variable">rs</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="comment">// 执行sql</span></span><br><span class="line">    rs = pstmt.executeQuery();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 设置？的值</span></span><br><span class="line">    pstmt.setString(<span class="number">1</span>,<span class="string">&quot;aaa&quot;</span>);</span><br><span class="line">    pstmt.setString(<span class="number">2</span>,<span class="string">&quot;bbb&quot;</span>);</span><br><span class="line">    <span class="comment">// 执行sql</span></span><br><span class="line">    rs = pstmt.executeQuery();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 判断登录是否成功</span></span><br><span class="line">    <span class="keyword">if</span>(rs.next())&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;登录成功~&quot;</span>);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        System.out.println(<span class="string">&quot;登录失败~&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//7. 释放资源</span></span><br><span class="line">    rs.close();</span><br><span class="line">    pstmt.close();</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


</li>
<li><p>执行SQL语句，查看 <code>D:\mysql.log</code> 日志如下:</p>
<p><img src= "" data-lazy-src="/assets/image-20210725202829738.png" alt="image-20210725202829738"></p>
<p>上图中第三行中的 <code>Prepare</code> 是对SQL语句进行预编译。第四行和第五行是执行了两次SQL语句，而第二次执行前并没有对SQL进行预编译。</p>
</li>
</ul>
<blockquote>
<p>&#x3D;&#x3D;小结：&#x3D;&#x3D;</p>
<ul>
<li>在获取PreparedStatement对象时，将sql语句发送给mysql服务器进行检查，编译（这些步骤很耗时）</li>
<li>执行时就不用再进行这些步骤了，速度更快</li>
<li>如果sql模板一样，则只需要进行一次检查、编译</li>
</ul>
</blockquote>
<h2 id="4，数据库连接池"><a href="#4，数据库连接池" class="headerlink" title="4，数据库连接池"></a>4，数据库连接池</h2><h3 id="4-1-数据库连接池简介"><a href="#4-1-数据库连接池简介" class="headerlink" title="4.1  数据库连接池简介"></a>4.1  数据库连接池简介</h3><blockquote>
<ul>
<li><p>数据库连接池是个容器，负责分配、管理数据库连接(Connection)</p>
</li>
<li><p>它允许应用程序重复使用一个现有的数据库连接，而不是再重新建立一个；</p>
</li>
<li><p>释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏</p>
</li>
<li><p>好处</p>
<ul>
<li>资源重用</li>
<li>提升系统响应速度</li>
<li>避免数据库连接遗漏</li>
</ul>
</li>
</ul>
</blockquote>
<p>之前我们代码中使用连接是没有使用都创建一个Connection对象，使用完毕就会将其销毁。这样重复创建销毁的过程是特别耗费计算机的性能的及消耗时间的。</p>
<p>而数据库使用了数据库连接池后，就能达到Connection对象的复用，如下图</p>
<img src= "" data-lazy-src="assets/image-20210725210432985.png" alt="image-20210725210432985" style="zoom:80%;" />

<p>连接池是在一开始就创建好了一些连接（Connection）对象存储起来。用户需要连接数据库时，不需要自己创建连接，而只需要从连接池中获取一个连接进行使用，使用完毕后再将连接对象归还给连接池；这样就可以起到资源重用，也节省了频繁创建连接销毁连接所花费的时间，从而提升了系统响应的速度。</p>
<h3 id="4-2-数据库连接池实现"><a href="#4-2-数据库连接池实现" class="headerlink" title="4.2  数据库连接池实现"></a>4.2  数据库连接池实现</h3><ul>
<li><p>标准接口：&#x3D;&#x3D;DataSource&#x3D;&#x3D;</p>
<p>官方(SUN) 提供的数据库连接池标准接口，由第三方组织实现此接口。该接口提供了获取连接的功能：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Connection <span class="title function_">getConnection</span><span class="params">()</span></span><br></pre></td></tr></table></figure>

<p>那么以后就不需要通过 <code>DriverManager</code> 对象获取 <code>Connection</code> 对象，而是通过连接池（DataSource）获取 <code>Connection</code> 对象。</p>
</li>
<li><p>常见的数据库连接池</p>
<ul>
<li>DBCP</li>
<li>C3P0</li>
<li>Druid</li>
</ul>
<p>我们现在使用更多的是Druid，它的性能比其他两个会好一些。</p>
</li>
<li><p>Druid（德鲁伊）</p>
<ul>
<li><p>Druid连接池是阿里巴巴开源的数据库连接池项目 </p>
</li>
<li><p>功能强大，性能优秀，是Java语言最好的数据库连接池之一</p>
</li>
</ul>
</li>
</ul>
<h3 id="4-3-Driud使用"><a href="#4-3-Driud使用" class="headerlink" title="4.3  Driud使用"></a>4.3  Driud使用</h3><blockquote>
<ul>
<li>导入jar包 druid-1.1.12.jar</li>
<li>定义配置文件</li>
<li>加载配置文件</li>
<li>获取数据库连接池对象</li>
<li>获取连接</li>
</ul>
</blockquote>
<p>现在通过代码实现，首先需要先将druid的jar包放到项目下的lib下并添加为库文件</p>
<img src= "" data-lazy-src="assets/image-20210725212911980.png" alt="image-20210725212911980" style="zoom:80%;" />

<p>项目结构如下：</p>
<img src= "" data-lazy-src="assets/image-20210725213210091.png" alt="image-20210725213210091" style="zoom:80%;" />

<p>编写配置文件如下：</p>
<figure class="highlight properties"><table><tr><td class="code"><pre><span class="line"><span class="attr">driverClassName</span>=<span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="attr">url</span>=<span class="string">jdbc:mysql:///db1?useSSL=false&amp;useServerPrepStmts=true</span></span><br><span class="line"><span class="attr">username</span>=<span class="string">root</span></span><br><span class="line"><span class="attr">password</span>=<span class="string">1234</span></span><br><span class="line"><span class="comment"># 初始化连接数量</span></span><br><span class="line"><span class="attr">initialSize</span>=<span class="string">5</span></span><br><span class="line"><span class="comment"># 最大连接数</span></span><br><span class="line"><span class="attr">maxActive</span>=<span class="string">10</span></span><br><span class="line"><span class="comment"># 最大等待时间</span></span><br><span class="line"><span class="attr">maxWait</span>=<span class="string">3000</span></span><br></pre></td></tr></table></figure>

<p>使用druid的代码如下：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Druid数据库连接池演示</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DruidDemo</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">        <span class="comment">//1.导入jar包</span></span><br><span class="line">        <span class="comment">//2.定义配置文件</span></span><br><span class="line">        <span class="comment">//3. 加载配置文件</span></span><br><span class="line">        <span class="type">Properties</span> <span class="variable">prop</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line">        prop.load(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;jdbc-demo/src/druid.properties&quot;</span>));</span><br><span class="line">        <span class="comment">//4. 获取连接池对象</span></span><br><span class="line">        <span class="type">DataSource</span> <span class="variable">dataSource</span> <span class="operator">=</span> DruidDataSourceFactory.createDataSource(prop);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//5. 获取数据库连接 Connection</span></span><br><span class="line">        <span class="type">Connection</span> <span class="variable">connection</span> <span class="operator">=</span> dataSource.getConnection();</span><br><span class="line">        System.out.println(connection); <span class="comment">//获取到了连接后就可以继续做其他操作了</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//System.out.println(System.getProperty(&quot;user.dir&quot;));</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5，JDBC练习"><a href="#5，JDBC练习" class="headerlink" title="5，JDBC练习"></a>5，JDBC练习</h2><h3 id="5-1-需求"><a href="#5-1-需求" class="headerlink" title="5.1  需求"></a>5.1  需求</h3><p>完成商品品牌数据的增删改查操作</p>
<ul>
<li>查询：查询所有数据</li>
<li>添加：添加品牌</li>
<li>修改：根据id修改</li>
<li>删除：根据id删除</li>
</ul>
<h3 id="5-2-案例实现"><a href="#5-2-案例实现" class="headerlink" title="5.2  案例实现"></a>5.2  案例实现</h3><h4 id="5-2-1-环境准备"><a href="#5-2-1-环境准备" class="headerlink" title="5.2.1  环境准备"></a>5.2.1  环境准备</h4><ul>
<li><p>数据库表 <code>tb_brand</code></p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="comment">-- 删除tb_brand表</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> if <span class="keyword">exists</span> tb_brand;</span><br><span class="line"><span class="comment">-- 创建tb_brand表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_brand (</span><br><span class="line">    <span class="comment">-- id 主键</span></span><br><span class="line">    id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">    <span class="comment">-- 品牌名称</span></span><br><span class="line">    brand_name <span class="type">varchar</span>(<span class="number">20</span>),</span><br><span class="line">    <span class="comment">-- 企业名称</span></span><br><span class="line">    company_name <span class="type">varchar</span>(<span class="number">20</span>),</span><br><span class="line">    <span class="comment">-- 排序字段</span></span><br><span class="line">    ordered <span class="type">int</span>,</span><br><span class="line">    <span class="comment">-- 描述信息</span></span><br><span class="line">    description <span class="type">varchar</span>(<span class="number">100</span>),</span><br><span class="line">    <span class="comment">-- 状态：0：禁用  1：启用</span></span><br><span class="line">    status <span class="type">int</span></span><br><span class="line">);</span><br><span class="line"><span class="comment">-- 添加数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> tb_brand (brand_name, company_name, ordered, description, status)</span><br><span class="line"><span class="keyword">values</span> (<span class="string">&#x27;三只松鼠&#x27;</span>, <span class="string">&#x27;三只松鼠股份有限公司&#x27;</span>, <span class="number">5</span>, <span class="string">&#x27;好吃不上火&#x27;</span>, <span class="number">0</span>),</span><br><span class="line">       (<span class="string">&#x27;华为&#x27;</span>, <span class="string">&#x27;华为技术有限公司&#x27;</span>, <span class="number">100</span>, <span class="string">&#x27;华为致力于把数字世界带入每个人、每个家庭、每个组织，构建万物互联的智能世界&#x27;</span>, <span class="number">1</span>),</span><br><span class="line">       (<span class="string">&#x27;小米&#x27;</span>, <span class="string">&#x27;小米科技有限公司&#x27;</span>, <span class="number">50</span>, <span class="string">&#x27;are you ok&#x27;</span>, <span class="number">1</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p>在pojo包下实体类 Brand</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 品牌</span></span><br><span class="line"><span class="comment"> * alt + 鼠标左键：整列编辑</span></span><br><span class="line"><span class="comment"> * 在实体类中，基本数据类型建议使用其对应的包装类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Brand</span> &#123;</span><br><span class="line">    <span class="comment">// id 主键</span></span><br><span class="line">    <span class="keyword">private</span> Integer id;</span><br><span class="line">    <span class="comment">// 品牌名称</span></span><br><span class="line">    <span class="keyword">private</span> String brandName;</span><br><span class="line">    <span class="comment">// 企业名称</span></span><br><span class="line">    <span class="keyword">private</span> String companyName;</span><br><span class="line">    <span class="comment">// 排序字段</span></span><br><span class="line">    <span class="keyword">private</span> Integer ordered;</span><br><span class="line">    <span class="comment">// 描述信息</span></span><br><span class="line">    <span class="keyword">private</span> String description;</span><br><span class="line">    <span class="comment">// 状态：0：禁用  1：启用</span></span><br><span class="line">    <span class="keyword">private</span> Integer status;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Integer <span class="title function_">getId</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setId</span><span class="params">(Integer id)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.id = id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getBrandName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> brandName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setBrandName</span><span class="params">(String brandName)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.brandName = brandName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getCompanyName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> companyName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setCompanyName</span><span class="params">(String companyName)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.companyName = companyName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Integer <span class="title function_">getOrdered</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> ordered;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setOrdered</span><span class="params">(Integer ordered)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.ordered = ordered;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getDescription</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> description;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setDescription</span><span class="params">(String description)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.description = description;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Integer <span class="title function_">getStatus</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> status;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setStatus</span><span class="params">(Integer status)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.status = status;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Brand&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;id=&quot;</span> + id +</span><br><span class="line">                <span class="string">&quot;, brandName=&#x27;&quot;</span> + brandName + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, companyName=&#x27;&quot;</span> + companyName + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, ordered=&quot;</span> + ordered +</span><br><span class="line">                <span class="string">&quot;, description=&#x27;&quot;</span> + description + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, status=&quot;</span> + status +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="5-2-2-查询所有"><a href="#5-2-2-查询所有" class="headerlink" title="5.2.2  查询所有"></a>5.2.2  查询所有</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 查询所有</span></span><br><span class="line"><span class="comment">   * 1. SQL：select * from tb_brand;</span></span><br><span class="line"><span class="comment">   * 2. 参数：不需要</span></span><br><span class="line"><span class="comment">   * 3. 结果：List&lt;Brand&gt;</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testSelectAll</span><span class="params">()</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">    <span class="comment">//1. 获取Connection</span></span><br><span class="line">    <span class="comment">//3. 加载配置文件</span></span><br><span class="line">    <span class="type">Properties</span> <span class="variable">prop</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line">    prop.load(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;jdbc-demo/src/druid.properties&quot;</span>));</span><br><span class="line">    <span class="comment">//4. 获取连接池对象</span></span><br><span class="line">    <span class="type">DataSource</span> <span class="variable">dataSource</span> <span class="operator">=</span> DruidDataSourceFactory.createDataSource(prop);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//5. 获取数据库连接 Connection</span></span><br><span class="line">    <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> dataSource.getConnection();</span><br><span class="line">    <span class="comment">//2. 定义SQL</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;select * from tb_brand;&quot;</span>;</span><br><span class="line">    <span class="comment">//3. 获取pstmt对象</span></span><br><span class="line">    <span class="type">PreparedStatement</span> <span class="variable">pstmt</span> <span class="operator">=</span> conn.prepareStatement(sql);</span><br><span class="line">    <span class="comment">//4. 设置参数</span></span><br><span class="line">    <span class="comment">//5. 执行SQL</span></span><br><span class="line">    <span class="type">ResultSet</span> <span class="variable">rs</span> <span class="operator">=</span> pstmt.executeQuery();</span><br><span class="line">    <span class="comment">//6. 处理结果 List&lt;Brand&gt; 封装Brand对象，装载List集合</span></span><br><span class="line">    <span class="type">Brand</span> <span class="variable">brand</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">    List&lt;Brand&gt; brands = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">    <span class="keyword">while</span> (rs.next())&#123;</span><br><span class="line">        <span class="comment">//获取数据</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">id</span> <span class="operator">=</span> rs.getInt(<span class="string">&quot;id&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">brandName</span> <span class="operator">=</span> rs.getString(<span class="string">&quot;brand_name&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">companyName</span> <span class="operator">=</span> rs.getString(<span class="string">&quot;company_name&quot;</span>);</span><br><span class="line">        <span class="type">int</span> <span class="variable">ordered</span> <span class="operator">=</span> rs.getInt(<span class="string">&quot;ordered&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">description</span> <span class="operator">=</span> rs.getString(<span class="string">&quot;description&quot;</span>);</span><br><span class="line">        <span class="type">int</span> <span class="variable">status</span> <span class="operator">=</span> rs.getInt(<span class="string">&quot;status&quot;</span>);</span><br><span class="line">        <span class="comment">//封装Brand对象</span></span><br><span class="line">        brand = <span class="keyword">new</span> <span class="title class_">Brand</span>();</span><br><span class="line">        brand.setId(id);</span><br><span class="line">        brand.setBrandName(brandName);</span><br><span class="line">        brand.setCompanyName(companyName);</span><br><span class="line">        brand.setOrdered(ordered);</span><br><span class="line">        brand.setDescription(description);</span><br><span class="line">        brand.setStatus(status);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//装载集合</span></span><br><span class="line">        brands.add(brand);</span><br><span class="line">    &#125;</span><br><span class="line">    System.out.println(brands);</span><br><span class="line">    <span class="comment">//7. 释放资源</span></span><br><span class="line">    rs.close();</span><br><span class="line">    pstmt.close();</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-2-3-添加数据"><a href="#5-2-3-添加数据" class="headerlink" title="5.2.3  添加数据"></a>5.2.3  添加数据</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 添加</span></span><br><span class="line"><span class="comment">  * 1. SQL：insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);</span></span><br><span class="line"><span class="comment">  * 2. 参数：需要，除了id之外的所有参数信息</span></span><br><span class="line"><span class="comment">  * 3. 结果：boolean</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testAdd</span><span class="params">()</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">    <span class="comment">// 接收页面提交的参数</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">brandName</span> <span class="operator">=</span> <span class="string">&quot;香飘飘&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">companyName</span> <span class="operator">=</span> <span class="string">&quot;香飘飘&quot;</span>;</span><br><span class="line">    <span class="type">int</span> <span class="variable">ordered</span> <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">description</span> <span class="operator">=</span> <span class="string">&quot;绕地球一圈&quot;</span>;</span><br><span class="line">    <span class="type">int</span> <span class="variable">status</span> <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//1. 获取Connection</span></span><br><span class="line">    <span class="comment">//3. 加载配置文件</span></span><br><span class="line">    <span class="type">Properties</span> <span class="variable">prop</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line">    prop.load(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;jdbc-demo/src/druid.properties&quot;</span>));</span><br><span class="line">    <span class="comment">//4. 获取连接池对象</span></span><br><span class="line">    <span class="type">DataSource</span> <span class="variable">dataSource</span> <span class="operator">=</span> DruidDataSourceFactory.createDataSource(prop);</span><br><span class="line">    <span class="comment">//5. 获取数据库连接 Connection</span></span><br><span class="line">    <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> dataSource.getConnection();</span><br><span class="line">    <span class="comment">//2. 定义SQL</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;insert into tb_brand(brand_name, company_name, ordered, description, status) values(?,?,?,?,?);&quot;</span>;</span><br><span class="line">    <span class="comment">//3. 获取pstmt对象</span></span><br><span class="line">    <span class="type">PreparedStatement</span> <span class="variable">pstmt</span> <span class="operator">=</span> conn.prepareStatement(sql);</span><br><span class="line">    <span class="comment">//4. 设置参数</span></span><br><span class="line">    pstmt.setString(<span class="number">1</span>,brandName);</span><br><span class="line">    pstmt.setString(<span class="number">2</span>,companyName);</span><br><span class="line">    pstmt.setInt(<span class="number">3</span>,ordered);</span><br><span class="line">    pstmt.setString(<span class="number">4</span>,description);</span><br><span class="line">    pstmt.setInt(<span class="number">5</span>,status);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//5. 执行SQL</span></span><br><span class="line">    <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> pstmt.executeUpdate(); <span class="comment">// 影响的行数</span></span><br><span class="line">    <span class="comment">//6. 处理结果</span></span><br><span class="line">    System.out.println(count &gt; <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//7. 释放资源</span></span><br><span class="line">    pstmt.close();</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-2-4-修改数据"><a href="#5-2-4-修改数据" class="headerlink" title="5.2.4  修改数据"></a>5.2.4  修改数据</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 修改</span></span><br><span class="line"><span class="comment">  * 1. SQL：</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">     update tb_brand</span></span><br><span class="line"><span class="comment">         set brand_name  = ?,</span></span><br><span class="line"><span class="comment">         company_name= ?,</span></span><br><span class="line"><span class="comment">         ordered     = ?,</span></span><br><span class="line"><span class="comment">         description = ?,</span></span><br><span class="line"><span class="comment">         status      = ?</span></span><br><span class="line"><span class="comment">     where id = ?</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">   * 2. 参数：需要，所有数据</span></span><br><span class="line"><span class="comment">   * 3. 结果：boolean</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testUpdate</span><span class="params">()</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">    <span class="comment">// 接收页面提交的参数</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">brandName</span> <span class="operator">=</span> <span class="string">&quot;香飘飘&quot;</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">companyName</span> <span class="operator">=</span> <span class="string">&quot;香飘飘&quot;</span>;</span><br><span class="line">    <span class="type">int</span> <span class="variable">ordered</span> <span class="operator">=</span> <span class="number">1000</span>;</span><br><span class="line">    <span class="type">String</span> <span class="variable">description</span> <span class="operator">=</span> <span class="string">&quot;绕地球三圈&quot;</span>;</span><br><span class="line">    <span class="type">int</span> <span class="variable">status</span> <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line">    <span class="type">int</span> <span class="variable">id</span> <span class="operator">=</span> <span class="number">4</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//1. 获取Connection</span></span><br><span class="line">    <span class="comment">//3. 加载配置文件</span></span><br><span class="line">    <span class="type">Properties</span> <span class="variable">prop</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line">    prop.load(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;jdbc-demo/src/druid.properties&quot;</span>));</span><br><span class="line">    <span class="comment">//4. 获取连接池对象</span></span><br><span class="line">    <span class="type">DataSource</span> <span class="variable">dataSource</span> <span class="operator">=</span> DruidDataSourceFactory.createDataSource(prop);</span><br><span class="line">    <span class="comment">//5. 获取数据库连接 Connection</span></span><br><span class="line">    <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> dataSource.getConnection();</span><br><span class="line">    <span class="comment">//2. 定义SQL</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot; update tb_brand\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;         set brand_name  = ?,\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;         company_name= ?,\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;         ordered     = ?,\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;         description = ?,\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;         status      = ?\n&quot;</span> +</span><br><span class="line">        <span class="string">&quot;     where id = ?&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//3. 获取pstmt对象</span></span><br><span class="line">    <span class="type">PreparedStatement</span> <span class="variable">pstmt</span> <span class="operator">=</span> conn.prepareStatement(sql);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//4. 设置参数</span></span><br><span class="line">    pstmt.setString(<span class="number">1</span>,brandName);</span><br><span class="line">    pstmt.setString(<span class="number">2</span>,companyName);</span><br><span class="line">    pstmt.setInt(<span class="number">3</span>,ordered);</span><br><span class="line">    pstmt.setString(<span class="number">4</span>,description);</span><br><span class="line">    pstmt.setInt(<span class="number">5</span>,status);</span><br><span class="line">    pstmt.setInt(<span class="number">6</span>,id);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//5. 执行SQL</span></span><br><span class="line">    <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> pstmt.executeUpdate(); <span class="comment">// 影响的行数</span></span><br><span class="line">    <span class="comment">//6. 处理结果</span></span><br><span class="line">    System.out.println(count &gt; <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//7. 释放资源</span></span><br><span class="line">    pstmt.close();</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-2-5-删除数据"><a href="#5-2-5-删除数据" class="headerlink" title="5.2.5  删除数据"></a>5.2.5  删除数据</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 删除</span></span><br><span class="line"><span class="comment">  * 1. SQL：</span></span><br><span class="line"><span class="comment">            delete from tb_brand where id = ?</span></span><br><span class="line"><span class="comment">  * 2. 参数：需要，id</span></span><br><span class="line"><span class="comment">  * 3. 结果：boolean</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testDeleteById</span><span class="params">()</span> <span class="keyword">throws</span> Exception &#123;</span><br><span class="line">    <span class="comment">// 接收页面提交的参数</span></span><br><span class="line">    <span class="type">int</span> <span class="variable">id</span> <span class="operator">=</span> <span class="number">4</span>;</span><br><span class="line">    <span class="comment">//1. 获取Connection</span></span><br><span class="line">    <span class="comment">//3. 加载配置文件</span></span><br><span class="line">    <span class="type">Properties</span> <span class="variable">prop</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Properties</span>();</span><br><span class="line">    prop.load(<span class="keyword">new</span> <span class="title class_">FileInputStream</span>(<span class="string">&quot;jdbc-demo/src/druid.properties&quot;</span>));</span><br><span class="line">    <span class="comment">//4. 获取连接池对象</span></span><br><span class="line">    <span class="type">DataSource</span> <span class="variable">dataSource</span> <span class="operator">=</span> DruidDataSourceFactory.createDataSource(prop);</span><br><span class="line">    <span class="comment">//5. 获取数据库连接 Connection</span></span><br><span class="line">    <span class="type">Connection</span> <span class="variable">conn</span> <span class="operator">=</span> dataSource.getConnection();</span><br><span class="line">    <span class="comment">//2. 定义SQL</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot; delete from tb_brand where id = ?&quot;</span>;</span><br><span class="line">    <span class="comment">//3. 获取pstmt对象</span></span><br><span class="line">    <span class="type">PreparedStatement</span> <span class="variable">pstmt</span> <span class="operator">=</span> conn.prepareStatement(sql);</span><br><span class="line">    <span class="comment">//4. 设置参数</span></span><br><span class="line">    pstmt.setInt(<span class="number">1</span>,id);</span><br><span class="line">    <span class="comment">//5. 执行SQL</span></span><br><span class="line">    <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> pstmt.executeUpdate(); <span class="comment">// 影响的行数</span></span><br><span class="line">    <span class="comment">//6. 处理结果</span></span><br><span class="line">    System.out.println(count &gt; <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//7. 释放资源</span></span><br><span class="line">    pstmt.close();</span><br><span class="line">    conn.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://aihcola.github.io">AIHCOLA</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://aihcola.github.io/2023/05/05/JDBC/">https://aihcola.github.io/2023/05/05/JDBC/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://aihcola.github.io" target="_blank">ai_喝可乐's Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaWeb/">JavaWeb</a><a class="post-meta__tags" href="/tags/JavaEE/">JavaEE</a><a class="post-meta__tags" href="/tags/JDBC/">JDBC</a></div><div class="post_share"><div class="social-share" data-image="https://picsum.photos/seed/d/3456/2304" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="next-post pull-full"><a href="/2023/03/05/Filter&amp;Listener&amp;AJAX/"><img class="next-cover" src= "" data-lazy-src="https://picsum.photos/seed/d/3456/2304" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Filter&amp;Listener&amp;AJAX</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/03/05/Filter&Listener&AJAX/" title="Filter&amp;Listener&amp;AJAX"><img class="cover" src= "" data-lazy-src="https://picsum.photos/seed/d/3456/2304" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-05</div><div class="title">Filter&amp;Listener&amp;AJAX</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#JDBC"><span class="toc-text">JDBC</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1%EF%BC%8CJDBC%E6%A6%82%E8%BF%B0"><span class="toc-text">1，JDBC概述</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-JDBC%E6%A6%82%E5%BF%B5"><span class="toc-text">1.1  JDBC概念</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-JDBC%E6%9C%AC%E8%B4%A8"><span class="toc-text">1.2  JDBC本质</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-JDBC%E5%A5%BD%E5%A4%84"><span class="toc-text">1.3  JDBC好处</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%EF%BC%8CJDBC%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8"><span class="toc-text">2，JDBC快速入门</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E7%BC%96%E5%86%99%E4%BB%A3%E7%A0%81%E6%AD%A5%E9%AA%A4"><span class="toc-text">2.1  编写代码步骤</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-%E5%85%B7%E4%BD%93%E6%93%8D%E4%BD%9C"><span class="toc-text">2.2  具体操作</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%EF%BC%8CJDBC-API%E8%AF%A6%E8%A7%A3"><span class="toc-text">3，JDBC API详解</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-DriverManager"><span class="toc-text">3.1  DriverManager</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-Connection"><span class="toc-text">3.2  Connection</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-1-%E8%8E%B7%E5%8F%96%E6%89%A7%E8%A1%8C%E5%AF%B9%E8%B1%A1"><span class="toc-text">3.2.1  获取执行对象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-2-%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86"><span class="toc-text">3.2.2  事务管理</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-Statement"><span class="toc-text">3.3  Statement</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-1-%E6%A6%82%E8%BF%B0"><span class="toc-text">3.3.1  概述</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-2-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">3.3.2  代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-ResultSet"><span class="toc-text">3.4  ResultSet</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-1-%E6%A6%82%E8%BF%B0"><span class="toc-text">3.4.1  概述</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-2-%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-text">3.4.2  代码实现</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-5-%E6%A1%88%E4%BE%8B"><span class="toc-text">3.5  案例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-6-PreparedStatement"><span class="toc-text">3.6  PreparedStatement</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-6-1-SQL%E6%B3%A8%E5%85%A5"><span class="toc-text">3.6.1  SQL注入</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-6-2-%E4%BB%A3%E7%A0%81%E6%A8%A1%E6%8B%9FSQL%E6%B3%A8%E5%85%A5%E9%97%AE%E9%A2%98"><span class="toc-text">3.6.2  代码模拟SQL注入问题</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-6-3-PreparedStatement%E6%A6%82%E8%BF%B0"><span class="toc-text">3.6.3  PreparedStatement概述</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-6-4-%E4%BD%BF%E7%94%A8PreparedStatement%E6%94%B9%E8%BF%9B"><span class="toc-text">3.6.4  使用PreparedStatement改进</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-6-5-PreparedStatement%E5%8E%9F%E7%90%86"><span class="toc-text">3.6.5  PreparedStatement原理</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%EF%BC%8C%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0"><span class="toc-text">4，数据库连接池</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E7%AE%80%E4%BB%8B"><span class="toc-text">4.1  数据库连接池简介</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E5%AE%9E%E7%8E%B0"><span class="toc-text">4.2  数据库连接池实现</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-Driud%E4%BD%BF%E7%94%A8"><span class="toc-text">4.3  Driud使用</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%EF%BC%8CJDBC%E7%BB%83%E4%B9%A0"><span class="toc-text">5，JDBC练习</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1-%E9%9C%80%E6%B1%82"><span class="toc-text">5.1  需求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2-%E6%A1%88%E4%BE%8B%E5%AE%9E%E7%8E%B0"><span class="toc-text">5.2  案例实现</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-1-%E7%8E%AF%E5%A2%83%E5%87%86%E5%A4%87"><span class="toc-text">5.2.1  环境准备</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-2-%E6%9F%A5%E8%AF%A2%E6%89%80%E6%9C%89"><span class="toc-text">5.2.2  查询所有</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-3-%E6%B7%BB%E5%8A%A0%E6%95%B0%E6%8D%AE"><span class="toc-text">5.2.3  添加数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-4-%E4%BF%AE%E6%94%B9%E6%95%B0%E6%8D%AE"><span class="toc-text">5.2.4  修改数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2-5-%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE"><span class="toc-text">5.2.5  删除数据</span></a></li></ol></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By AIHCOLA</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>